package activiti2;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.List;

/**
 * 请假测试
 */
public class QingjiaTest {
    /**
     * 获得流程引擎
     */
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /**
     * 部署流程定义
     */
    @Test
    public void deployProcessDefinition(){

        Deployment deployment = processEngine.getRepositoryService()//获取流程实例
                .createDeployment()//创建部署对象
                .addClasspathResource("diagrams/helloworld.bpmn")
                .deploy();
        System.out.println("部署ID：" + deployment.getId());//部署id
        System.out.println("部署时间：" + deployment.getDeploymentTime());//部署时间
    }

    /**
     * 启动流程定义
     */
    @Test
    public void startProcessInstance(){

        String ProcessDefinitionKey = "myProcess_2";//流程定义的Key，也就是bpmn中存在的ID
        ProcessInstance pi = processEngine.getRuntimeService()//管理流程实例和执行对象，也就是表示正在执行的操作
                .startProcessInstanceByKey(ProcessDefinitionKey);////按照流程定义的key启动流程实例
        System.out.println("流程实例ID：" + pi.getId());
        System.out.println("流程实例ID：" + pi.getProcessInstanceId());
        System.out.println("流程实例ID：" + pi.getProcessDefinitionId());

    }

    /**
     * 查看个人任务
     */
    @Test
    public void findPersonnelTaskList(){
        String assignee = "张三";//当前任务办理人
        List<Task> tasks = processEngine.getTaskService()//与任务相关的Service
                .createTaskQuery()//创建一个任务查询对象
                .taskAssignee(assignee)
                .list();
        if(tasks != null && tasks.size() > 0){
            for (Task task : tasks) {
                System.out.println("任务ID:" + task.getId());
                System.out.println("任务的办理人：" + task.getAssignee());
                System.out.println("任务名称：" + task.getName());
                System.out.println("任务的创建时间：" + task.getCreateTime());
                System.out.println("流程实例ID：" + task.getProcessInstanceId());
                System.out.println("---------------------------------------------------");
            }
        }

    }

    /**
     * 完成任务
     */

    @Test
    public void completeTask(){
        String taskID = "7502";
        processEngine.getTaskService().complete(taskID);
        System.out.println("完成任务：" + taskID);
    }


}
